﻿#include "dialog.h"
#include <QComboBox>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSerialPortInfo>
#include <QSpinBox>
#include <QDebug>
#include <QThread>

// 45 52 52 4F 52 ERROR
Dialog::Dialog(QWidget *parent)
    : QDialog(parent),
    m_transactionCount(0),
    m_waitResponseLabel(new QLabel(tr("Wait response, msec:")))
{
     QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
     if(infos.empty())
     {
         QMessageBox MyBox(QMessageBox::Question,"tip","no use port",QMessageBox::Yes|QMessageBox::No);
         MyBox.exec();
     }
     else
     {
         // 这个信号和曹 简单来说 由系统触发
        //connect(&m_serial,&QSerialPort::readyRead,this,&Dialog::readResponse);
        initData();
         m_serial.setDataBits(QSerialPort::Data8);
         m_serial.setStopBits(QSerialPort::OneStop);
         m_serial.setParity(QSerialPort::NoParity);
         m_serial.setFlowControl(QSerialPort::NoFlowControl);

        for(int i=0;i<infos.size();i++)
        {
            if(m_serial.portName()!=infos.at(i).portName())
            {
                m_serial.close();
                m_serial.setPortName(infos.at(i).portName());
            }
           bool flag=true;
           for(int j=0;j<4 && flag;j++)
           {
              switch(j)
              {
              case 0:
                  m_serial.setBaudRate(QSerialPort::Baud19200);
                  if(!m_serial.open(QIODevice::ReadWrite)){
                      qDebug()<<"not is open1";
                      return;
                  }
                  sendRequest();

                  pTimer.start(200);
                  connect(&m_serial,&QSerialPort::readyRead,this,&Dialog::readResponse);
                  connect(&pTimer,SIGNAL(timeout()),this,SLOT(readResponse()));
                  QThread::sleep(10);
                  //connect(&m_serial,&QSerialPort::readyRead,this,&Dialog::readResponse);
                  break;
              case 1:
                  m_serial.setBaudRate(QSerialPort::Baud9600);
                  m_serial.close();
                  if(!m_serial.open(QIODevice::ReadWrite)){
                      qDebug()<<"not is open2";
                      return;
                  }

                  sendRequest();
                 // readResponse();
                  // connect(&m_serial,&QSerialPort::readyRead,this,&Dialog::readResponse);
                  break;
              case 2:
                  m_serial.setBaudRate(QSerialPort::Baud38400);
                  m_serial.close();
                  if(!m_serial.open(QIODevice::ReadWrite)){
                      qDebug()<<"not is open3";
                      return;
                  }

                  sendRequest();
                  //readResponse();
                  //  connect(&m_serial,&QSerialPort::readyRead,this,&Dialog::readResponse);
                  break;
              case 3:
                  m_serial.setBaudRate(QSerialPort::Baud115200);
                  m_serial.close();
                  if(!m_serial.open(QIODevice::ReadWrite)){
                      qDebug()<<"not is ope"
                                "n4";
                      return;
                  }

                  sendRequest();
                  connect(&m_timer,&QTimer::timeout,this,&Dialog::readResponse);
                 // readResponse();
                  // connect(&m_serial,&QSerialPort::readyRead,this,&Dialog::readResponse);
                  break;
              }
           }
        }
     }
    // 单次定时器
    m_timer.setSingleShot(true);
    //sendRequest();
    connect(&m_timer,&QTimer::timeout,this,&Dialog::processTimeout);
}

void Dialog::sendRequest()
{
    QByteArray localFromHex;
    for(int i=0;i<DataMsg.size();i++)
    {
        localFromHex = QByteArray::fromHex(DataMsg.at(i));
        qDebug()<<"localFromHex="<<localFromHex;
        m_serial.write(localFromHex);
    }
    // 等待时长
    m_timer.start(400);
}

bool Dialog::isRealBound()
{
     QByteArray localReadAll = m_serial.readAll().toHex();
      m_response.append(localReadAll);
      if(m_response.isEmpty())
          return false;
      qDebug()<<"m_response="<<m_response;
      emit sendData(m_response);
}

void Dialog::readResponse()
{
    qDebug()<<"++i"<<++i;
    QByteArray localReadAll = m_serial.readAll().toHex();
    qDebug()<<"localReadAll="<<localReadAll;
    m_response.append(localReadAll);
    if(m_response.isEmpty())
    {
    }
    qDebug()<<"m_response="<<m_response;
    emit sendData(m_response);
}
void Dialog::initData()
{
    DataMsg<<"EB900701000000000000000000000000000000";
    //DataMsg<<"EB900800000000000000000000000000000000";
    //DataMsg<<"EB900700000000000000000000000000000000";

}


void Dialog::processTimeout()
{
    qDebug()<<"processTimeout.m_response="<<m_response;
    data=QString::fromUtf8(m_response);
}












